package com.etc

import org.apache.spark.mllib.classification.NaiveBayes
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.{SparkConf, SparkContext}


/**
  * 朴素贝叶斯算法
  */
object NaiveBayesdemo {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("NaiveBayes").setMaster("local[2]")
    val sc = new SparkContext(conf)



    val data = sc.textFile("football.txt")

    val parsedData = data.map{ line =>
      val parts = line.split(',')
      LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
    }

    val Array(traning,test) = parsedData.randomSplit(Array(0.6, 0.4), seed = 11L)

    val model = NaiveBayes.train(traning,lambda = 1.0,modelType = "multinomial")


    val predictionAndLabel = test.map(p => (model.predict(p.features),p.label))


    val tuples = predictionAndLabel.take(20)


    for (i <- 0 to tuples.length - 1){
      println(tuples(i)._1 + "\t" + tuples(i)._2)
    }

    println("Predictionof (0.0, 2.0, 0.0, 1.0):"+model.predict(Vectors.dense(0.0,2.0,0.0,1.0)))


  }

}
